<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty Channel 与 ChannelPipeline 深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            position: relative;
            font-family: 'Consolas', 'Monaco', monospace;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            color: #888;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .section-title {
            position: relative;
            padding-left: 1rem;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(0.8); opacity: 0.5; }
            50% { transform: scale(1.2); opacity: 0.8; }
        }
        
        .flow-step {
            position: relative;
            padding-left: 3rem;
            margin-bottom: 1.5rem;
        }
        
        .flow-step::before {
            content: attr(data-step);
            position: absolute;
            left: 0;
            top: 0;
            width: 2rem;
            height: 2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 0.875rem;
        }
        
        .concept-card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .concept-card:hover {
            border-color: #667eea;
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.2);
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
        
        .keyword {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-8 relative">
        <div class="max-w-6xl mx-auto relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    <i class="fas fa-network-wired mr-4"></i>
                    Netty 核心组件深度解析
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">Channel 与 ChannelPipeline 的设计精髓与源码剖析</p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div><i class="fas fa-code mr-2"></i>源码分析</div>
                    <div><i class="fas fa-sitemap mr-2"></i>架构设计</div>
                    <div><i class="fas fa-rocket mr-2"></i>性能优化</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-8 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Netty 的世界里，<code class="keyword">Channel</code> 和 <code class="keyword">ChannelPipeline</code> 
                如同网络通信的双子星，共同构建了高性能网络应用的基石。Channel 代表着网络连接的抽象，
                而 ChannelPipeline 则是处理数据流的核心管道，两者相辅相成，共同实现了 Netty 优雅而高效的事件驱动模型。
            </p>
        </div>

        <!-- Architecture Overview -->
        <div class="highlight-box mb-12">
            <h2 class="text-2xl font-bold mb-4"><i class="fas fa-lightbulb mr-3"></i>核心架构概览</h2>
            <p class="text-lg relative z-10">
                Netty 采用了责任链模式设计 ChannelPipeline，每个 Channel 都拥有独立的 Pipeline，
                数据在其中按序流经多个 ChannelHandler，实现了灵活的数据处理流程。
            </p>
        </div>

        <!-- Mermaid Diagram -->
        <div class="mermaid">
            <div class="mermaid">
                graph TB
                    A[客户端连接] -->|建立| B[Channel]
                    B --> C[ChannelPipeline]
                    C --> D[ChannelHandler 1]
                    D --> E[ChannelHandler 2]
                    E --> F[ChannelHandler N]
                    
                    subgraph "Inbound 事件流"
                        G[读取数据] --> H[解码]
                        H --> I[业务处理]
                    end
                    
                    subgraph "Outbound 事件流"
                        J[业务数据] --> K[编码]
                        K --> L[写入网络]
                    end
                    
                    F -.-> G
                    I -.-> J
                    
                    style A fill:#f093fb,stroke:#f5576c,stroke-width:2px
                    style B fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
                    style C fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Channel Section -->
        <section class="mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-plug mr-3 text-purple-600"></i>Channel：网络连接的抽象
            </h2>
            
            <div class="concept-card mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-700">核心概念</h3>
                <p class="text-gray-600 leading-relaxed mb-6">
                    Channel 是 Netty 对网络套接字的抽象封装，它代表了一个到实体（如硬件设备、文件、网络套接字等）的开放连接，
                    提供了统一的 API 来进行网络 I/O 操作。
                </p>
                
                <div class="code-block">
                    <pre><code>public interface Channel extends EventLoopGroup {
    // 获取当前 Channel 的管道
    ChannelPipeline pipeline();
    
    // 写数据到网络
    ChannelFuture write(Object msg);
    
    // 写入并刷新数据
    ChannelFuture writeAndFlush(Object msg);
    
    // 关闭连接
    ChannelFuture close();
}</code></pre>
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                <div class="concept-card">
                    <h4 class="text-lg font-semibold mb-3 text-purple-700">
                        <i class="fas fa-key mr-2"></i>关键特性
                    </h4>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>异步非阻塞的 I/O 操作</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>支持多种传输类型（NIO、OIO、Local、Embedded）</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>提供了丰富的配置选项</li>
                        <li><i class="fas fa-check-circle mr-2 text-green-500"></i>与 EventLoop 紧密集成</li>
                    </ul>
                </div>
                
                <div class="concept-card">
                    <h4 class="text-lg font-semibold mb-3 text-purple-700">
                        <i class="fas fa-cogs mr-2"></i>生命周期
                    </h4>
                    <div class="space-y-3">
                        <div class="flow-step" data-step="1">
                            <strong>ChannelRegistered</strong> - Channel 注册到 EventLoop
                        </div>
                        <div class="flow-step" data-step="2">
                            <strong>ChannelActive</strong> - Channel 处于活跃状态，可以收发数据
                        </div>
                        <div class="flow-step" data-step="3">
                            <strong>ChannelInactive</strong> - Channel 不再活跃
                        </div>
                        <div class="flow-step" data-step="4">
                            <strong>ChannelUnregistered</strong> - Channel 从 EventLoop 注销
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- ChannelPipeline Section -->
        <section class="mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-stream mr-3 text-purple-600"></i>ChannelPip